<template>
	<scroll-view scroll-y="true" class="all" @scrolltolower="loadMore">
		<view class="all__header">
			<view class="header__item">{{$t('stock.name')}}</view>
			<view class="header__item" @click="typeChange('zhangdieshu')">{{$t('stock.price')}}
				<image src="@/static/icons/order_icon.png" mode="widthFix" class="item__icon"></image>
			</view>
			<view class="header__item" @click="typeChange('zhangdiebaifenbi')">{{$t('stock.riseFall')}}
				<image src="@/static/icons/order_icon.png" mode="widthFix" class="item__icon"></image>
			</view>
		</view>
		<view :class="['all__item', item.zhangdiebaifenbi >= 0 ?'all__item--add':'']" 
		v-for="(item, index) in marketList" :key="index"  @click="itemClick(item)">
			<view class="item__name">
				<view class="name__text">{{item.name}}</view>
				<view class="name__info">
					<image :src="iconUrl" mode="aspectFill" class="info__icon"></image>
					<text class="info__tag">{{item.shuzidaima | padValue}}</text>
					<!-- <text class="info__code">023356</text> -->
				</view>
			</view>
			<view class="item__price">
								<view class="price__num">{{item.price | format}}</view>

				<!-- <text class="price__count">4.930</text> -->
			</view>
			<view class="item__increa">
				<text class="increa__num">{{item.zhangdiebaifenbi | formatPercent}}</text>
			</view>
		</view>
	</scroll-view>

</template>

<script>
	export default{
		data(){
			return {
				id:"",
				pageIndex:1,
				marketList:[],
				isLoadAll:false, //是否加载完成
				priceOlder:"desc", //价格排序方式 desc //降序 asc升序
				increaseOlder:"", //涨幅排序
				file:"", //国家分类名称
				orderName:"", //当前排序的字段 zhangdieshu 价格  zhangdiebaifenbi涨跌
				order:"",  // desc  降序  asc 升序
			}
		},
		filters:{
			// 格式化百分比
			formatPercent(float){
				let num = float ? float : 0
				return num.toFixed(2) + "%"
			},
			padValue(value){
				if(typeof value === 'number'){
					return value.toString().padStart(5, '0')
				}
				return value
			},
						format(float){
							return float.toFixed(2)
						}

		},
		computed:{
			iconUrl(){
				let file = this.file  //KRN US HK
				return `/static/icons/${file}.png`
			}
		},
		onLoad(options) {
			this.id = options.id
			this.file = options.file
			this.getMarketList()
		},
		methods:{
			// 加载更多
			loadMore(){
				if(this.isLoadAll){
					uni.showToast({
						title:this.$t('empty.tips'),
						icon:"none"
					})
					return
				}
				this.pageIndex ++
				this.getMarketList()
			},
			// 获取市场股票列表
			async getMarketList(){
				uni.showLoading({
					title:this.$t('loading.loading'),
					mask:true
				})
				
				let {url} = '';
				if(this.id == 1){
					 url = 'https://api.investing.com/api/financialdata/assets/equitiesByCountry/default?fields-list=id%2Cname%2Csymbol%2CisCFD%2Chigh%2Clow%2Clast%2ClastPairDecimal%2Cchange%2CchangePercent%2Cvolume%2Ctime%2CisOpen%2Curl%2Cflag%2CcountryNameTranslated%2CexchangeId%2CperformanceDay%2CperformanceWeek%2CperformanceMonth%2CperformanceYtd%2CperformanceYear%2Cperformance3Year%2CtechnicalHour%2CtechnicalDay%2CtechnicalWeek%2CtechnicalMonth%2CavgVolume%2CfundamentalMarketCap%2CfundamentalRevenue%2CfundamentalRatio%2CfundamentalBeta%2CpairType&country-id=35&page='+this.pageIndex+'&page-size=100&include-major-indices=false&include-additional-indices=false&include-primary-sectors=false&include-other-indices=false&limit=0';
				}
				if(this.id == 2){
					 url = 'https://api.investing.com/api/financialdata/assets/equitiesByCountry/default?fields-list=id%2Cname%2Csymbol%2CisCFD%2Chigh%2Clow%2Clast%2ClastPairDecimal%2Cchange%2CchangePercent%2Cvolume%2Ctime%2CisOpen%2Curl%2Cflag%2CcountryNameTranslated%2CexchangeId%2CperformanceDay%2CperformanceWeek%2CperformanceMonth%2CperformanceYtd%2CperformanceYear%2Cperformance3Year%2CtechnicalHour%2CtechnicalDay%2CtechnicalWeek%2CtechnicalMonth%2CavgVolume%2CfundamentalMarketCap%2CfundamentalRevenue%2CfundamentalRatio%2CfundamentalBeta%2CpairType&country-id=5&page='+this.pageIndex+'&page-size=100&include-major-indices=false&include-additional-indices=false&include-primary-sectors=false&include-other-indices=false&limit=0';
					
				}
				if(this.id == 3){
					 url = 'https://api.investing.com/api/financialdata/assets/equitiesByCountry/default?fields-list=id%2Cname%2Csymbol%2CisCFD%2Chigh%2Clow%2Clast%2ClastPairDecimal%2Cchange%2CchangePercent%2Cvolume%2Ctime%2CisOpen%2Curl%2Cflag%2CcountryNameTranslated%2CexchangeId%2CperformanceDay%2CperformanceWeek%2CperformanceMonth%2CperformanceYtd%2CperformanceYear%2Cperformance3Year%2CtechnicalHour%2CtechnicalDay%2CtechnicalWeek%2CtechnicalMonth%2CavgVolume%2CfundamentalMarketCap%2CfundamentalRevenue%2CfundamentalRatio%2CfundamentalBeta%2CpairType&country-id=4&page='+this.pageIndex+'&page-size=100&include-major-indices=false&include-additional-indices=false&include-primary-sectors=false&include-other-indices=false&limit=0';
				}
				
				uni.request({
				    url: url,
					method: 'GET',
				    data: {},
				    success: (res) => {
				        uni.hideLoading()
						let total = res.data.total
						let list = res.data?.data || []
						
						
						this.marketList  = list.map(item=>{
							item.id = item.Id
							item.codeName = item.Symbol
							item.category_id = 1
							item.close = item.Low
							item.code = item.Symbol
							item.discount = item.Low
							item.high = item.High
							item.low = item.Low
							item.name = item.Name
							item.open = item.Last
							item.price = item.Last
							item.shichang = item.Flag
							item.shuzidaima = item.Symbol
							item.turnover = item.Symbol
							item.turnover_section = item.Symbol
							item.vol = item.Volume
							item.zhangdiebaifenbi = item.ChgPct
							item.zhangdieshu = item.Chg
							item.zimudaima = item.Symbol
							item.zongshizhi = item.FundamentalMarketCap
							return item
						})
						
				    },
				    fail: (err) => {
				        console.error(err); // 请求失败的错误信息
				    }
				});
				// let res = await this.$api.getMarketList(this.id, this.pageIndex, this.orderName, this.order)
				// uni.hideLoading()
				// if(res.code === 1 && res.data?.data){
				// 	let total = res.data.total
				// 	let list = res.data?.data || []
				// 	this.marketList = this.marketList.concat(list)
				// 	if(this.marketList.length >= total){
				// 		this.isLoadAll= true
				// 	}
				// }
			},
			itemClick(item){
				uni.navigateTo({
					url:`/pages/account/stockDetail?id=${item.id}&info=`+encodeURIComponent(JSON.stringify(item))
				})
			},
			// 价格排序切换
			typeChange(type){
				this.orderName = type
				this.order = this.order === "desc" ? "asc" :"desc"
				this.pageIndex = 1
				this.marketList = []
				this.getMarketList()
			},
		}
	}
</script>

<style lang="scss">
	.all{
		padding:  0 30rpx;
		box-sizing: border-box;
		height: 100vh;
		.all__header{
			@include flexRowAlign(space-between);
			height: 64rpx;
			margin-top: 20rpx;
			.header__item{
				@include flexRowAlign(center);
				font-size: 28rpx;
				color: #999999;
				width: 220rpx;
				.item__icon{
					width: 24rpx;
					height: 28rpx;
					margin-left: 4rpx;
				}
				&:nth-child(2){
					flex: 1;
				}
				&:first-child{
					justify-content: flex-start;
				}
				&:last-child{
					flex: 1;
					justify-content: flex-end;
				}
			}
		}
		.all__item{
			@include flexRowAlign(space-between);
			height: 138rpx;
			// border-top: 1rpx solid #EEEEEE;
			.item__name{
				@include flexCenter(column);
				align-items: flex-start;
				.name__text{
					@include textOver();
					width: 220rpx;
					font-size: 28rpx;
					color: #fff;
				}
				.name__info{
					@include flexRowAlign()
					margin-top: 10rpx;
					.info__icon{
						width: 30rpx;
						height: 30rpx;
						border-radius: 50%;
						margin-right: 10rpx;
					}
					.info__tag{
						color: #fff;
						font-size: 24rpx;
						// background: #FF3B46;
						@include flexCenter();
					}
					.info__code{
						font-size: 20rpx;
						font-weight: 500;
						color: #FFFFFF;
						margin-left: 10rpx;
					}
				}
			}
			.item__price{
				@include flexCenter(column);
				flex: 1;
				.price__num{
					font-size: 28rpx;
					font-weight: 800;
					color: #2DC065;
				}
				.price__count{
					font-size: 24rpx;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
			.item__increa{
				@include flexRowAlign(flex-end);
				flex: 1;
				.increa__num{
					min-width: 150rpx;
					height: 48rpx;
					background: rgba(#2DC065, 0.5);
					border-radius: 8rpx;
					padding:  0 10rpx;
					@include flexCenter(column);
					font-size: 28rpx;
					font-weight: 800;
					color: #2DC065;
				}
			}
		}
		.all__item--add{
			.item__price{
				.price__num{
					color: #FF3B46;
				}
			}
			.item__increa{
				.increa__num{
					background: rgba(#FF3B46, 0.5);
					color: #FF3B46;
				}
			}
		}
	}
</style>